<template>
	<w-layout showNavbar navTitle="商城" :navTitleColor="publicStyles['--color-t1']" background="url('/static/image/bk.png') no-repeat"
		navBackgroundColor="transparent" :scrollTop="page.scrollTop" :navScrollColor="publicStyles['--color-bk']">
		<template #default>
			<view class="container">
				
				<view class="px-60">
					<up-tabs
						@change="tabChange"
						:list="tabs.list" 
						:current="tabs.current"
						lineWidth="14"
						lineHeight="14"
						:lineColor="`url('/static/icon/public/top-fill.png') 100% 100%`"
						:activeStyle="{
							color: publicStyles['--color'],
							fontWeight: 'bold',
							transform: 'scale(1.2)'
						}"
						:scrollable="false"
						itemStyle="height: 60px;"
					>
						<template #content="{item, keyName, index}">
							<view class="fr-ac">
								<view class="fs-32" :class="index == tabs.current?'color':''">
									{{item[keyName]}}
								</view>
								<image v-if="index == 1 && tabs.type == 0" class="ml-10" style="width: 24rpx;height: 24rpx;" src="/static/icon/public/sort.png" mode=""></image>
								<image v-if="index == 1 && tabs.type == 1" class="ml-10" style="width: 24rpx;height: 24rpx;" src="/static/icon/public/sort-asc.png" mode=""></image>
								<image v-if="index == 1 && tabs.type == 2" class="ml-10" style="width: 24rpx;height: 24rpx;" src="/static/icon/public/sort-desc.png" mode=""></image>
							</view>
						</template>
					</up-tabs>
				</view>
				
				<view class="gap-30 mt-40" style="display: grid;grid-template-columns: repeat(2,1fr);">
					<view @click="$u.route(`/pages/shopping/goods_detail?id=${item.id}`)" class="public-card-5 fc px-30 py-30" style="min-height: 450rpx;position: relative;" v-for="(item,index) in state.list" :key="index">
						<image style="width: 100%;height: 250rpx;" :src="item.image" mode="widthFix"></image>
						<view class="mt-30 fs-24" style="line-height: 1.6;">
							{{item.title}}
						</view>
						<view class="color fs-32 mt-auto">
							¥{{item.price}}
						</view>
						
						<view v-if="item.is_popularity" class="fs-24 px-20 py-8" style="position: absolute;top: 0;left: 0;background: var(--color);color: #002C33;">
							人气新品
						</view>
					</view>
				</view>
				<no-more :show="state.list.length == 0"></no-more>
				
				
			</view>
		</template>
	</w-layout>
</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted,
		computed,
		nextTick
	} from 'vue'
	import {
		storeToRefs
	} from 'pinia'
	import {
		onLoad, onPageScroll,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		useAppStore
	} from '@/store/app'
	import {
		useColorStore
	} from '@/store/color'
	import {
		shop_seckill_product
	} from '@/utils/request/api';
	import {
		useUserStore
	} from '@/store/user'
	const userStore = useUserStore()
	const appStore = useAppStore()
	const colorStore = useColorStore()
	const {
		publicStyles
	} = storeToRefs(colorStore)
	onLoad(async(options) => {
		get_shop_seckill_product()
	})
	onMounted(() => {
		appStore.loadingHandler(false)
	})
	onPageScroll((e)=>{
		page.scrollTop = e.scrollTop
	})
	onReachBottom(()=>{
		if(state.last_limit == state.limit){
			state.page++
			get_shop_seckill_product()
		}
	})
	const page = reactive({
		scrollTop:0
	})
	
	const tabs = reactive({
		current:0,
		list:[
			{
				id:0,
				name:'综合排序',
			},
			{
				id:1,
				name:'价格',
			},
			{
				id:2,
				name:'上新',
			}
		],
		type: 0,
	})
	function tabChange(e){
		tabs.current = e.index
		console.log(tabs.current)
		if(tabs.current == 0){
			tabs.type = 0
		}
		if(tabs.current == 2){
			tabs.type = 2
		}
		if(tabs.current == 1){
			switch (tabs.type){
				case 0:
					// 升序
					tabs.type = 1
					break;
				case 1:
					// 降序
					tabs.type = 2
					break;
				case 2:
					// 无序
					tabs.type = 1
					break;
				default:
					break;
			}
		}
		initData()
		get_shop_seckill_product()
	}
	
	const state = reactive({
		list:[],
		page:1,
		limit:15,
		last_limit:15,
		
		keyword:'',
	})
	function initData(){
		state.list = []
		state.page = 1
		state.last_limit = 15
	}
	
	async function get_shop_seckill_product(){
		const res = await shop_seckill_product({
			state: tabs.type,
			page: state.page
		})
		state.last_limit = res.data.length
		state.list = state.list.concat(res.data)
	}
</script>

<style lang="scss">
	.shadow {
		box-shadow: 2rpx 2rpx 12rpx var(--shadow);
	}
	.color{
		color: var(--color);
	}

	.container {
		width: 100%;
		min-height: 100%;
		position: relative;
		overflow: hidden;
		box-sizing: border-box;
		padding: 0 30rpx;
		
	}
</style>